import express from 'express';
import Ret, { RetCode, SUCCESS } from '../../bean/code/Ret';
const app = express();
import { Op } from 'Sequelize'
import { db, Sequelize } from '../../dao/config';
import { converPage } from '../../bean/page';
import { excludeEmpty } from '../../utils';
import logger from '../../utils/logger';
const UserPoints = require("../../dao/models/f_user_out_points")(db, Sequelize);
const User = require("../../dao/models/f_user")(db, Sequelize);
const UserRealInfo = require("../../dao/models/f_user_real_info")(db, Sequelize);

UserPoints.belongsTo(User, {
    foreignKey: "user_id",
    as: "user"
})
User.hasOne(UserRealInfo, {
    foreignKey: "user_id",
    as: "userRealInfo"
})
app.post("/outPoints/getAllOutPointsRequest", async (req, res) => {
    let params = req.body, { page = {} } = params, { pageSize = 10, pageNum = 1 } = page, { realInfo = {} } = params;
    const condition = excludeEmpty(params, UserPoints);
    const realInfoCondition = excludeEmpty(realInfo, UserRealInfo);
    if (params.createTimeRange) {
        condition.createTime = { [Op.between]: params.createTimeRange }
    }
    const userPoints = await UserPoints.findAndCount({
        where: condition,
        include: { model: User, include: { model: UserRealInfo, as: "userRealInfo", where: realInfoCondition }, as: "user", required: true }, order: [
            ['createTime', 'DESC'],
        ], ...converPage(pageSize, pageNum)
    });
    res.json(Ret(SUCCESS).setData(userPoints));
})
 

export default app;
